//入口程序
//引入http服务模块
const config = require("./config/config");
const httpServer = require("./service/httpServer");
const { exec } = require("child_process");
var openBrowser = require("child_process");

// const WriteDayYeild=require("../ld_z2_tm/db/db")
// WriteDayYeild.ldz2FristWrite()
// 集中设计包头
httpServer.on("request", function (req, res) {
  if (req.url === "/plain") {
    res.setHeader("Content-Type", "text/plain;charset=utf-8");
    // res.end("你好 世界");
  } else if (req.url === "/html") {
    res.setHeader("Content-Type", "text/html;charset=utf-8");
    // res.end("<h1>你好 世界<br/> hello world</h1>");
  }
  res.header("Access-Control-Allow-Credentials", "true");
  //   res.setHeader("Content-Type", "text/html; charset=utf-8");
});
//启动web服务
httpServer.listen(config.port);

// 引入udp服务和tcp socket服务模块
// const udpServer = require('./service/serial')

// 获取本地ip并打开浏览器
const os = require("os");
///获取本机ip///
function getIPAdress() {
  var interfaces = os.networkInterfaces();
  for (var devName in interfaces) {
    var iface = interfaces[devName];
    for (var i = 0; i < iface.length; i++) {
      var alias = iface[i];
      if (alias.family === "IPv4" && alias.address !== "127.0.0.1" && !alias.internal) {
        // console.log(alias)
        switch (process.platform) {
          //mac系统使用 一下命令打开url在浏览器
          case "darwin":
            exec(`open ${config.host}` + ":" + `${config.port}`);
          // win系统使用 一下命令打开url在浏览器
          case "win32":
            // exec(`start  ` + `http://` + `${alias.address}`);
            openBrowser.exec("start http://" + `${config.host}` + ":" + `${config.port}`);
          // 默认mac系统
          default:
            exec(`open ${alias.address}`);
        }
        // 使用默认浏览器打开
        return alias.address;
      }
    }
  }
}
const myHost = getIPAdress();

// ==========================
// var iconv = require("iconv-lite");
// var fs = require("fs");
// var fileStr = fs.readFileSync("input.txt", { encoding: "binary" });
